bash
This demonstrates the use of brace expansion in Bash to generate sequences of numbers and letters, while noting the limitation of not being able to use variables within brace expansions.
echo {1..10} # => 1 2 3 4 5 6 7 8 9 10 echo {a..z} # => a b c d e f g h i j k l m n o p q r s t u v w x y z # This will output the range from the start value to the end value. # Note that you can't use variables here: from=1 to=10 echo {$from..$to} # => {$from..$to} echo {{1..3},{7..9}} # => 1 2 3 7 8 9
bash internaldata manipulationsstring manipulation and expansionsparameter expansionbrace expansion
bash
This snippet demonstrates file path manipulation using parameter expansion in Bash, including removing file extensions, extracting directory paths, getting file extensions, and replacing substrings.
str="/path/to/foo.cpp" echo "$str" # /path/to/foo (variable expansion) echo "${str}" # /path/to/foo (variable expansion) echo '$str' # '$str' (disabled variable expansion) echo "${str%.cpp}" # /path/to/foo echo "${str%.cpp}.o" # /path/to/foo.o echo "${str%/*}" # /path/to echo "${str##*.}" # cpp (extension) echo "${str##*/}" # foo.cpp (basepath) echo "${str#*/}" # path/to/foo.cpp echo "${str##*/}" # foo.cpp echo "${str/foo/bar}" # /path/to/bar.cpp
bash internaldata manipulationsstring manipulation and expansionsparameter expansionpath manipulation
bash
This demonstrates the usage of positional arguments in Bash scripts and functions.
$1 # first argument passed to a script or function $# # the number of arguments passed to a script or functio $* # all the arguments passed to a script or function, treating them as a single string $@ # all the arguments passed to a script or function, treating them as an array
bash internaldata manipulationspositional argumentspositional argument
bash
This demonstrates the usage of the printf
command in Bash for formatted output, including strings, integers, floats, and writing to files.
printf "Hello %s, I'm %s" Sven Olga #=> "Hello Sven, I'm Olga printf "1 + 1 = %d" 2 #=> "1 + 1 = 2" printf "This is how you print a float: %f" 2 #=> "This is how you print a float: 2.000000" printf '%s\n' '#!/bin/bash' 'echo hello' >file # format string is applied to each group of arguments printf '%i+%i=%i\n' 1 2 3 4 5 9
bash internaldata manipulationsstring manipulation and expansionsformatted output
bash
This demonstrates various methods of redirecting stdout
and stderr
in Bash, including appending, discarding, and combining streams.
python hello.py > output.txt # stdout to (file) python hello.py >> output.txt # stdout to (file), append python hello.py 2> error.log # stderr to (file) python hello.py 2>&1 # stderr to stdout python hello.py 2>/dev/null # stderr to (null) python hello.py >output.txt 2>&1 # stdout and stderr to (file), equivalent to &> python hello.py &>/dev/null # stdout and stderr to (null) echo "$0: warning: too many users" >&2 # print diagnostic message to stderr
bash internalfile and stream operationsstream redirection and pipingstdout redirection